//
//  ViewController.m
//  UI13_豆瓣
//
//  Created by dllo on 16/3/25.
//  Copyright © 2016年 XWH. All rights reserved.
//

#import "ViewController.h"
#import "DoubanTableViewCell.h"
#import "JSONParser.h"
#import "DouBanModel.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *dataSource;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    [self handleDataSource];
}

-(void)creatView{
    
    UITableView *tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableview.delegate = self;
    tableview.dataSource = self;
    
    UINib *nib = [UINib nibWithNibName:@"DoubanTableViewCell" bundle:[NSBundle mainBundle]];
    
    [tableview registerNib:nib forCellReuseIdentifier:@"DoubanTableViewCellIdentifier"];
    
    [self.view addSubview:tableview];
    [tableview release];
    
}


-(void)handleDataSource{
    
    id result = [JSONParser JSONParserWithFileName:@"activitylist" type:@"txt"];
    
    self.dataSource = [NSMutableArray array];
    
    for (NSDictionary *dic in result[@"events"]) {
        DouBanModel *model = [[DouBanModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.dataSource addObject:model];
    }
    
    NSLog(@"%@", self.dataSource);
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DoubanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DoubanTableViewCellIdentifier"];
    
    DouBanModel *model = self.dataSource[indexPath.row];
    cell.model = model;
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 400;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
